package com.yowasa.xiuxian.engine.battle.strategy.drop.all;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.BS;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.battle.strategy.BaseSpellStrategy;
import com.yowasa.xiuxian.entity.bo.battle.BattleContent;
import com.yowasa.xiuxian.entity.bo.battle.BuffContent;
import com.yowasa.xiuxian.entity.bo.battle.UserBattleContent;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.BattleHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;

@BS(name = "双剑华斩")
@Order(503)
@Component
public class ShuangJianHuaZhan extends BaseSpellStrategy {
    @Autowired
    private BattleHelper battleHelper;

    @Override
    public ElementEnum getElement() {
        return null;
    }

    @Override
    public void action(UserBattleContent first, UserBattleContent second, BattleContent content) {
        Integer saveRound = (Integer) first.getFlag().get("saveKnife");
        Integer useRound = (Integer) first.getFlag().get("useKnife");

        if(saveRound == null){
            //首次
            first.getFlag().put("saveKnife",content.getTurn());

            BuffContent buffContent = new BuffContent();
            buffContent.setDuration(3);
            buffContent.setNum(1);
            second.getBuff().put("藏刀", buffContent);
            content.getMsgLog().add(first.getUserBO().getName()+"收刀入鞘，做出防御姿势开始蓄力");
        }else {
            if(useRound == null){
                //二次
                first.getFlag().put("useKnife",content.getTurn());
                first.getBuff().remove("藏刀");

                int atk = first.getBattleInfo().getAtk();
                int def = (int) (second.getBattleInfo().getDef() * 0.5);
                int dmg = (int) (battleHelper.calAtk(atk, def) * 3.5);
                battleHelper.autoAction(this, first, second, content, dmg, "双剑华斩");
            }else {
                content.getMsgLog().add("双剑华斩error");
            }
        }


    }

    @Override
    public void limit(UserBO user) {
        if (user.getStage() < 4) {
            throw new HandlerException("学习/使用双剑华斩阶位需要在了彻之白以上");
        }
        if (user.getElement().get("金") < XiuxianConstants.DEFAULT_ELEMENT_ACTIVITY) {
            throw new HandlerException("学习/使用双剑华斩必须具有金灵根");
        }
    }

    @Override
    public String limitDesc() {
        return "学习/使用双剑华斩阶位需要在了彻之白以上，且拥有金灵根";
    }

    @Override
    public String desc() {
        return "稻妻秘剑「天狗抄」流派的绝技居合斩，经机关剑鬼改良后使用双剑[孤风][霜驰]释放而出，威力更甚从前\n刀剑入鞘进入居合状态，双抗翻倍，物攻减少60%，3回合后释放出去，造成3.5倍的物理伤害（以原攻击力结算,无视50%防御）\n" +
                "释放后居合状态结束，属性回归常值，居合状态中可以使用其他战技，本战技每次战斗仅能使用一次 ";
    }

    @Override
    public boolean check(UserBattleContent user, BattleContent content) {
        Integer saveRound = (Integer) user.getFlag().get("saveKnife");
        Integer useRound = (Integer) user.getFlag().get("useKnife");
        if (saveRound != null) {
            if (useRound != null) {
                return false;
            }
            if (content.getTurn() - saveRound <= 3) {
                return false;
            }
        }
        return true;
    }
}
